package com.example.helloworldtext2;

import android.content.Intent;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;

import androidx.activity.EdgeToEdge;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import androidx.fragment.app.Fragment;

import com.example.helloworldtext2.activities.AddActivity;
import com.example.helloworldtext2.fragments.CircleFragment;
import com.example.helloworldtext2.fragments.HomeFragment;
import com.example.helloworldtext2.fragments.ProfileFragment;
import com.example.helloworldtext2.fragments.VideoFragment;
import com.google.android.material.bottomnavigation.BottomNavigationView;

public class MainActivity extends AppCompatActivity implements BottomNavigationView.OnItemSelectedListener {

    private BottomNavigationView bottomNavigationView;
    private View addButton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_main);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });

        bottomNavigationView = findViewById(R.id.bottom_navigation);
        addButton = findViewById(R.id.add_button);
        
        bottomNavigationView.setOnItemSelectedListener(this);
        
        // 设置发布按钮点击事件
        addButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this, AddActivity.class);
                startActivity(intent);
            }
        });
        
        // 设置第一个View的底部边距
        View firstMenuItem = bottomNavigationView.getChildAt(0);
        if (firstMenuItem != null) {
            ViewGroup.MarginLayoutParams layoutParams = (ViewGroup.MarginLayoutParams) firstMenuItem.getLayoutParams();
            layoutParams.bottomMargin = dpToPx(12);
            firstMenuItem.setLayoutParams(layoutParams);
        }
        
        // 默认显示首页
        loadFragment(new HomeFragment());
    }

    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem item) {
        Fragment fragment = null;
        int itemId = item.getItemId();
        
        if (itemId == R.id.menu_home) {
            fragment = new HomeFragment();
        } else if (itemId == R.id.menu_video) {
            fragment = new VideoFragment();
        } else if (itemId == R.id.menu_add) {
            // 发布按钮被点击
            Intent intent = new Intent(this, AddActivity.class);
            startActivity(intent);
            return true;
        } else if (itemId == R.id.menu_circle) {
            fragment = new CircleFragment();
        } else if (itemId == R.id.menu_profile) {
            fragment = new ProfileFragment();
        }

        if (fragment != null) {
            loadFragment(fragment);
            return true;
        }
        
        return false;
    }

    private void loadFragment(Fragment fragment) {
        getSupportFragmentManager()
                .beginTransaction()
                .replace(R.id.fragment_container, fragment)
                .commit();
    }
    
    private int dpToPx(int dp) {
        return (int) (dp * getResources().getDisplayMetrics().density);
    }
}